package com.yfw.maple_leaf_store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfw.maple_leaf_store.common.BaseContext;
import com.yfw.maple_leaf_store.common.R;
import com.yfw.maple_leaf_store.domain.Goods;
import com.yfw.maple_leaf_store.domain.OrderDetail;
import com.yfw.maple_leaf_store.domain.Orders;
import com.yfw.maple_leaf_store.domain.ShoppingCart;
import com.yfw.maple_leaf_store.dto.OrderCollectDto;
import com.yfw.maple_leaf_store.dto.OrderDto;
import com.yfw.maple_leaf_store.service.OrderDetailService;
import com.yfw.maple_leaf_store.service.OrderService;
import com.yfw.maple_leaf_store.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: yfw
 * @date: 2022/6/18 23:51
 * com.yfw.maple_leaf_store.controller
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 用户下单
     * @param orderCollectDto
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody OrderCollectDto orderCollectDto){
        log.info("订单数据：{}",orderCollectDto);
        orderService.submit(orderCollectDto);
        return R.success("下单成功");
    }

    //抽离的一个方法，通过订单id查询订单明细，得到一个订单明细的集合
    //这里抽离出来是为了避免在stream中遍历的时候直接使用构造条件来查询导致eq叠加，从而导致后面查询的数据都是null
    public List<OrderDetail> getOrderDetailListByOrderId(Long orderId){
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOid, orderId);
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);
        return orderDetailList;
    }

    /**
     * 用户端展示自己的订单分页查询
     * @param current
     * @param size
     * @return
     * 直接从分页对象中获取订单id就行，问题大大简化了......
     */
    @GetMapping("/userPage")
    public R<Page> page(Integer current, Integer size){
        //分页构造器对象
        Page<Orders> pageInfo = new Page<>(current,size);
        Page<OrderDto> pageDto = new Page<>(current,size);
        //构造条件查询对象
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Orders::getUid, BaseContext.getCurrentId());
        queryWrapper.eq(Orders::getUid, Long.valueOf("1538364171261296641"));//测试

        //这里是直接把当前用户分页的全部结果查询出来，要添加用户id作为查询条件，否则会出现用户可以查询到其他用户的订单情况
        //添加排序条件，根据更新时间降序排列
        queryWrapper.orderByDesc(Orders::getOrderTime);//根据最新的下单时间排序
        orderService.page(pageInfo,queryWrapper);

        //通过OrderId查询对应的OrderDetail
        LambdaQueryWrapper<OrderDetail> queryWrapper2 = new LambdaQueryWrapper<>();

        //对OrderDto进行需要的属性赋值
        List<Orders> records = pageInfo.getRecords();
        List<OrderDto> orderDtoList = records.stream().map((item) ->{
            OrderDto orderDto = new OrderDto();
            //此时的orderDto对象里面orderDetails属性还是空 下面准备为它赋值
            Long orderId = item.getId();//获取订单id
            List<OrderDetail> orderDetailList = this.getOrderDetailListByOrderId(orderId);//根据订单id查询订单明细
            BeanUtils.copyProperties(item,orderDto);//拷贝对象
            //对orderDto进行OrderDetails属性的赋值
            orderDto.setOrderDetails(orderDetailList);
            return orderDto;
        }).collect(Collectors.toList());
        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        pageDto.setRecords(orderDtoList);
        return R.success(pageDto);
    }

}
